package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.TestingParamKit;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import javax.swing.JOptionPane;

/**
 * 
 * CloseRequestController holds all methods for handaling the close request stage
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * 
 */
public class CloseRequestController{

	private TestingParamKit myKit=TestingParamKit.getTestingKit();
	/**
	 * sends the server the request decision and notifies user about status
	 * shows message after database update
	 * @param req - the request about to be closed
	 * @param requsetInfoFrame - the request frame for this request
	 */
	public void closeAndNotifyUser(ChangeRequest req,final RequsetInfoFrame requsetInfoFrame) {
		try {
			myKit.mailSent=false;
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(ButtonPermission.SupervisorCloseAndNotify,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (myKit.TestMode){
						requsetInfoFrame.dispose();
						myKit.mailSent=true;
						myKit.notifyTester();
					}
					else{
						if (mssg.is(P.error)) {
							JOptionPane.showMessageDialog(requsetInfoFrame, "Request closed and waiting for user confirmation!");
							requsetInfoFrame.dispose();
							return;
						}
					
						else {
						
							JOptionPane.showMessageDialog(requsetInfoFrame,"Request closed and waiting for user confirmation!");
							requsetInfoFrame.dispose();
							requsetInfoFrame.dispose();
							}
						}					
					}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(requsetInfoFrame,"cant connect to DB");
		}
		
	}
/**
 * executes the user confirmation for close request stage
 * shows message after database update
 * @param req - the request about to be closed
 * @param requsetInfoFrame  - the frame for this request
 */
	public void execUserConfirmation(ChangeRequest req,final RequsetInfoFrame requsetInfoFrame) {
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(ButtonPermission.UserConfirm,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (myKit.TestMode){
						myKit.notifyTester();
						requsetInfoFrame.dispose();
						requsetInfoFrame.dispose();

					}
					else{
						if (mssg.is(P.error)) {
							JOptionPane.showMessageDialog(requsetInfoFrame,"cant update close to DB");
							return;
						}
					
						else {
						
							JOptionPane.showMessageDialog(requsetInfoFrame,"Confirmation updated, Thank you!");
							requsetInfoFrame.dispose();
							requsetInfoFrame.dispose();
						}
						}

					}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(requsetInfoFrame,"cant connect to DB");
		}
		
	}






}
